<?php
/**
 * Created by PhpStorm.
 * User: JSQ-SL
 * Date: 2018/11/19
 * Time: 15:37
 */

namespace app\index\table;

use app\index\controller\Login;
use think\Db;
use app\index\service\ExcelService;

class LiquidTable extends BaseTable
{
    protected static $view = 'V_LIQUID_INFO';
    protected static $table = 'LIQUID_INFO';
    protected static $pk = 'LIQUID_NO';
    protected static $configTable = 'LIQUID_CONFIG';

    static public function table(){
        return Db::table(static::$view);
    }

    static public function add($data){
        $data['INPUT_USER_ID'] = Login::getUserId();
        $data['INPUT_TIME'] = date('Y-m-d');
        self::toDate($data,'INPUT_TIME');
        Db::table(static::$table)->insert($data);
    }

    static public function addImport($data){
        $data['INPUT_USER_ID'] = Login::getUserId();
        $data['INPUT_TIME'] = date('Y-m-d');
        self::toDate($data,'INPUT_TIME');
        Db::table(static::$table)->insert($data);
    }


    static public function update($where,$data){
        Db::table(static::$table)->where($where)->update($data);
    }

    static public function delete($where){
        return Db::table(static::$table)->where($where)->delete();
    }

    public static function getConfigAdd(){
        return Db::table(self::$configTable)->where(['ADD_FLAG'=>'1'])->order('ID ASC')->select();
    }

    public static function getConfigEdit(){
        return Db::table(self::$configTable)->where(['EDIT_FLAG'=>'1'])->order('ID ASC')->select();
    }

    public static function getConfigPrint(){
        return Db::table(self::$configTable)->where(['PRINT_FLAG'=>'1'])->order('ID ASC')->select();
    }

//    public static function getNextId(){
//        $year = date('Y');
//        $numMaxRecord = Db::table(static::$table)
//            ->field('max(SUBSTR('.self::$pk.',5,4)) as NUM')
//            ->where([['SUBSTR('.self::$pk.',1,4)','=',$year]])
//            ->fetchSql(true)
//            ->find();
//        $numMaxRecord = Db::query($numMaxRecord);
//        $num = $numMaxRecord[0]['NUM'];
//        if ($num == NULL) {
//            $num = 0;
//        }
//        $num++;
//        return $year.sprintf("%04d", $num);
//    }

    public static function exportExcel($data,$fileName){

        $fieldList = [
            ['title'=>'结算日期','field'=>'LIQUID_DATE'],
            ['title'=>'资产类型','field'=>'LIQUID_TYPE'],
            ['title'=>'期初账面数','field'=>'QC_ZM'],
            ['title'=>'期末账面数','field'=>'QM_ZM'],
            ['title'=>'期末实有数','field'=>'QM_SY'],
            ['title'=>'制单人','field'=>'INPUT_NAME'],
            ['title'=>'制单时间','field'=>'INPUT_TIME'],
        ];

        $excel = new ExcelService();
        $excel->exportExcel($data,$fileName,$fieldList);
    }

    const LIQUID_FIELD_LIST = [
        "LIQUID_DATE","LIQUID_TYPE_ID","QC_ZM","QM_ZM","QM_SY","INPUT_USER_ID","INPUT_TIME"
    ];

    public static function getSessionConditions() {
        $condition = null;
        $listCondition = Login::getListConditionLiquid();

        if (!empty($listCondition['LIQUID_TYPE_ID'])) {
            $value = $listCondition['LIQUID_TYPE_ID'];
            $condition[] = ['LIQUID_TYPE_ID','=',$value];
        }

        if (!empty($listCondition['QC_ZM_START'])) {
            $value = $listCondition['QC_ZM_START'];
            $condition[] = ['QC_ZM','>=',$value];
        }
        if (!empty($listCondition['QC_ZM_END'])) {
            $value = $listCondition['QC_ZM_END'];
            $condition[] = ['QC_ZM','<=',$value];
        }

        if (!empty($listCondition['QM_ZM_START'])) {
            $value = $listCondition['QM_ZM_START'];
            $condition[] = ['QM_ZM','>=',$value];
        }
        if (!empty($listCondition['QM_ZM_END'])) {
            $value = $listCondition['QM_ZM_END'];
            $condition[] = ['QM_ZM','<=',$value];
        }

        if (!empty($listCondition['QM_SY_START'])) {
            $value = $listCondition['QM_SY_START'];
            $condition[] = ['QM_SY','>=',$value];
        }
        if (!empty($listCondition['QM_SY_END'])) {
            $value = $listCondition['QM_SY_END'];
            $condition[] = ['QM_SY','<=',$value];
        }
        return $condition;
    }



}